package cn.sciento.transfer.infra.migration.job;

import java.util.List;
import java.util.Map;
import cn.sciento.boot.scheduler.infra.annotation.JobHandler;
import cn.sciento.boot.scheduler.infra.enums.ReturnT;
import cn.sciento.boot.scheduler.infra.handler.IJobHandler;
import cn.sciento.boot.scheduler.infra.tool.SchedulerTool;
import cn.sciento.transfer.app.service.DbMigrationLogService;
import cn.sciento.transfer.domain.entity.DbMigrationLog;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;

@JobHandler("dbMigrationJob")
public class DBMigrationJob implements IJobHandler {
  @Autowired
  private DbMigrationLogService dbMigrationLogService;
  
  public ReturnT execute(Map<String, String> map, SchedulerTool schedulerTool) {
    List<DbMigrationLog> dbMigrationLogList = this.dbMigrationLogService.probeDbMigrations();
    if (CollectionUtils.isEmpty(dbMigrationLogList))
      schedulerTool.info("no data."); 
    dbMigrationLogList.forEach(dbMigrationLog -> {
          String updateDetail = this.dbMigrationLogService.doExecuteDbMigration(dbMigrationLog.getDbMigrationLogId());
          schedulerTool.info("execute detail: " + updateDetail);
        });
    return ReturnT.SUCCESS;
  }
}
